package com.lobobrowser.parser;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.StringWriter;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;

import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import com.lobobrowser.encoder.Encoder;
import com.lobobrowser.encoder.Key;
import com.sun.org.apache.xerces.internal.parsers.DOMParser;

public class JSPFileParser {

	private File file;
	private Key key;
	private Encoder encoder;
	
	private static String iframe = "iframe";
	private static String body = "head";
	
	public JSPFileParser(File f){
		this.file = f;
		key = new Key(3);
		encoder = new Encoder(key);
		
	}
	
	public void parse(){
		
		
		  DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		  DOMParser parser = new DOMParser();
		  try {
			
			  parser.parse(file.getAbsolutePath());
			
			  
			DocumentBuilder db = dbf.newDocumentBuilder();
//			XMLDocument doc = parser.getDocument();
			Document doc = db.parse(file); 
			doc.getDocumentElement().normalize();
			
			javax.xml.transform.TransformerFactory tfactory = TransformerFactory.newInstance();
			javax.xml.transform.Transformer xform = tfactory.newTransformer();
			javax.xml.transform.Source src = new DOMSource(doc);
			
			java.io.FileOutputStream writer = new FileOutputStream(file);
			StringWriter strwriter = new StringWriter();
			
//			javax.xml.transform.Result strresult = new javax.xml.transform.stream.StreamResult(strwriter);
			javax.xml.transform.Result result = new javax.xml.transform.stream.StreamResult(writer);
			
			//put in key
			NodeList list = doc.getDocumentElement().getElementsByTagName(body);
			System.out.println(key.getKey());
			String kTag = Key.KEY_TAG + key.getKey();
			System.out.println(kTag);
			Node keyNode = doc.createElement( kTag );
			list.item(0).appendChild(keyNode);
			
			//iframe
			 list =doc.getDocumentElement().getElementsByTagName(iframe);
			
			for(int i = 0; i < list.getLength(); i++){
				 doc.renameNode(list.item(i), "", Encoder.ENCSTR + encoder.encode(list.item(i).getNodeName()));
			}
			
			xform.transform(src, result);
//			xform.transform(src, strresult);
			
			System.out.println(strwriter.toString());
			
			System.out.println("Done");

//			print (doc, 0);
			
			
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (TransformerConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (TransformerException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	

	
	public static void main(String [] args){
		System.out.println("Entering");
		JSPFileParser p = new JSPFileParser( new File("C:/dev/workspace/testwebapp/WebContent/index.jsp"));
		p.parse();
	}
}
